<template>
  <div class="mod-config">
    <table-view ref="view"
                :searchAttrs="searchAttrs"
                :primaryKey="primaryKey"
                :listApiName="listApiName"
                :sumInfo='sumInfo'
                :showAttrs="showAttrs"
    >
      <template v-slot:header>
<!--        <el-button size="small" type="warning" @click="openChartDialog">图表</el-button>-->
<!--        <el-button size="small" type="success" @click="exportClientWeightInfo" >导出所有库存</el-button>-->
      </template>
      <template v-slot:foot="{scope}">
        <el-tooltip class="item" effect="dark" content="调平库存" placement="bottom" :hide-after="1800" :open-delay="800">
<!--         -->
          <el-popconfirm
            title="确认调平库存数量？"
            @confirm="autoAdjustmentInventory(scope.row)"
          >
            <el-button slot="reference" size="small" type="text"  >调平库存</el-button>
          </el-popconfirm>

        </el-tooltip>
<!--        <el-tooltip class="item" effect="dark" content="导出库存" placement="bottom" :hide-after="1800" :open-delay="800">
          <el-button size="small" type="text" @click="showClientVisible(R.path(['row', primaryKey], scope))">
            导出
          </el-button>
        </el-tooltip>-->
      </template>
    </table-view>

  </div>
</template>

<script>
import tableView from '@/components/tableView'
import basicMixins from '@/utils/commonViewMixins'
import radarChart from '@/views/charts/client/clientOrderRadarChart'
import {mRequest} from '@/utils/request'
import {getFormatDateTime,exportHandle,isAuth} from '@/utils'

export default {
  mixins: [basicMixins],
  data() {
    return {
      inventoryDataList: [],
      inventoryLoading: false,
      inventoryDialog: false,
      clientOrderStorageExportDialogVisible: false,
      sumInfo: {countNumber: 'countNumber',totalPrice:'totalPrice'},

      searchForm: {},
      pageIndex: 1,
      pageSize: 50,
      totalPage: 0,
      selectClientId: '',
      productId: '',
      productInfoList: [],
      dataListSelections: [],
      clientDateTimeRange: '',
      chartSearchObj: {
        dataForm: {
          dateTimeRange: []
        },
        config: {
          dateTimeRange: {
            type: 'date',
            clearable: true,
            dateType: 'daterange',
            label: '时间范围'
          }
        }
      },
      searchAttrs: {
        productNo: {
          label: '产品名称',
          clearable: true,
          placeholder: '请输入产品名称'
        },
        clientName: {
          label: '客户名称',
          clearable: true,
          placeholder: '请输入客户名称'
        },
        saleName: {
          label: '销售人员',
          clearable: true,
          placeholder: '请输入销售人员'
        },
        // dateTimeRange: {
        //   type: 'date',
        //   label: '时间范围',
        //   dateType: 'datetimerange',
        //   defaultTime: ['06:00:00', '08:00:00'],
        //   clearable: true
        // },
      },
      primaryKey: 'id',
      listApiName: 'productApi/getProductCountNumberPage',
      // deleteApiName: 'clientApi/deleteClientInfo',
      // saveAuthName: 'client:clientinfo:save',
      // deleteAuthName: 'client:clientinfo:delete',
      // templateDownloadApiName: 'clientApi/downloadClientInfo',
     // importApiName: 'clientApi/importClientInfo',
      showAttrs: {
        productNo: {label: '产品名称',
          width: 150,
          type: 'linkLabel',
          routerName: 'product-product-info-all',
          searchKey: 'productNo'},
        productWeight: {label: '克重',width: 120},
        countNumber: {label: '结余数量'},
        unitPrice: {label: '单价'},
        totalPrice: {label: '总价'},
        clientName: {label: '客户'},
        saleName: {label: '销售'},
        putInStorageNumber: {label: '入库数量', width: 220},
        leaveStorageNumber: {label: '出库数量', width: 220},

      }
    }
  },
  components: {
    tableView,
    radarChart
  },
  methods: {
    autoAdjustmentInventory(obj) {
      if (obj.countNumber > 0) {
        let productLeaveStorage={
          'productId':obj.productId,
          'realSaleNumber': Math.abs(obj.countNumber)
        }
        mRequest('productApi/saveLeaveStorageByAutoAdjustment', productLeaveStorage, (data) => {
          if (data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500
            })

          } else {
            this.$message.error(data.msg)
          }

        })
      }else {
        let productPutStorage={
          'productId':obj.productId,
          'countProductNumber': Math.abs(obj.countNumber)
        }
        mRequest('productApi/savePutInStorageByAutoAdjustment', productPutStorage, (data) => {
          if (data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500
            })

          } else {
            this.$message.error(data.msg)
          }

        })
      }
      setTimeout(() => {
        this.getDataList()
      }, 1000)

    },
    showInventory(id) {
      this.pageIndex=1
      this.selectClientId = id
      this.inventoryDialog = true
      this.inventoryLoading = true
      this.productId = ''
      this.getDataListBySelectClient()
      this.initProductInfoList()
    },
    initProductInfoList() {
      mRequest('productApi/getProductInfoList', {}, (res) => {
        this.productInfoList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    getDataListBySelectClient() {
      let params = {
        clientId: this.selectClientId,
        productId: this.productId,
        page: this.pageIndex,
        limit: this.pageSize
      }
      mRequest('orderApi/getOrderDetailPageByClientId', params, (res) => {
        const list = this.R.pathOr([], ['data', 'page','list'], res)
        const totalCount = this.R.pathOr([], ['data','page', 'totalCount'], res)
        this.totalPage = totalCount
        this.inventoryDataList = this.R.map((item) => ({
          orderDetailId: this.R.propOr('', 'id', item),
          orderDetailNo: this.R.propOr('', 'orderNo', item),
          orderProductNumber: this.R.propOr('0', 'orderProductNumber', item),
          productId: this.R.propOr('', 'productId', item),
          productNo: this.R.propOr(0, 'productNo', item),
          productWeight: this.R.propOr(0, 'productWeight', item),
          putInStorageValue: this.R.propOr(0, 'putInStorageValue', item),
          leaveStorageValue: this.R.propOr(0, 'leaveStorageValue', item),
          allocationFromValue: this.R.propOr(0, 'allocationFromValue', item),
          allocationToValue: this.R.propOr(0, 'allocationToValue', item),
          lossLightBottleValue: this.R.propOr(0, 'lossLightBottleValue', item),
          inventory: this.R.propOr(0, 'putInStorageValue', item)+
            this.R.propOr(0, 'allocationToValue', item) -
            this.R.propOr(0, 'leaveStorageValue', item)-
            this.R.propOr(0, 'allocationFromValue', item)-
            this.R.propOr(0, 'lossLightBottleValue', item)
        }), list)
        this.inventoryLoading = false
      })
    },
    asLossProductNumber() {
      let lossLightBottleInfoListVo={
        'clientId':this.selectClientId,
        'lossLightBottleInfoList': this.dataListSelections
      }
      console.log(lossLightBottleInfoListVo)
      mRequest('lossApi/saveMultipleLossLightBottleInfo', lossLightBottleInfoListVo, (data) => {
        if (data.data.code === 0) {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500
          })

        } else {
          this.$message.error(data.data.msg)
        }
        this.getDataListBySelectClient()
      })

    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataListBySelectClient()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataListBySelectClient()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val

    },
    showClientVisible(id) {
      this.clientDateTimeRange = ''
      this.selectClientId = id
      this.clientOrderStorageExportDialogVisible = true

    },
    exportClientWeightInfo() {
      let startDateTime = ''
      let endDateTime = ''
      if (this.clientDateTimeRange instanceof Array) {
        startDateTime = getFormatDateTime(new Date(this.clientDateTimeRange[0]))
        endDateTime  = getFormatDateTime(new Date(this.clientDateTimeRange[1]))
      }
      exportHandle('orderApi/exportOrderStorageListByClientId', {
        startDateTime: startDateTime,
        endDateTime: endDateTime,
        clientId: this.selectClientId
      })
      this.selectClientId = null
      this.clientOrderStorageExportDialogVisible = false
    },
  }
}
</script>
